prolog './g128x.ijs'
NB. 128!:0 and 128!:1 ---------------------------------------------------

a  =: ?19 4$100
qr =: 128!:0 a
q =: >0{qr
r =: >1{qr

($q) -: $a
($r) -: 2$1{$a

i =: =i.#r
1e_13>>./|,(a+0=a)%~a-q+/ . *r
1e_14>>./|,i - (|:q) +/ . * q
*./,(|*r)<:<:/~i.#r

s =: 128!:1 r
($r) -: $s
1e_14>>./|,i-r +/ . * s

a  =: j.&?~ 9 5$100
qr =: 128!:0 a
q =: >0{qr
r =: >1{qr

($q) -: $a
($r) -: 2$1{$a

i =: =i.#r
1e_13>>./|,(a+0=a)%~a-q+/ . * r
1e_14>>./|,i - (+|:q) +/ . * q
*./,(|*r)<:<:/~i.#r

s =: 128!:1 r
($r) -: $s
1e_14>>./|,i-r +/ . * s

'domain error' -: ex '128!:88'

1e_12 > >./ ,| (%. - 128!:1) 3 3 $ _1 2 _4 0 5 _7 0 0 _7.
1e_12 > >./ ,| (%. - 128!:1) 3 3 $ _1 2 _4 0 5 _7 0 0 _7




'NaN error' -: 128!:1 etx 4 4 $ 0. 0 0 1

NB. 128!:2 --------------------------------------------------------------

(+/   x) -: '+/'   128!:2 x=: ?3 4 5$1e9
(+/"1 x) -: '+/"1' 128!:2 x

NB. 128!:10 -------------------------------------------------------------

todiag =: ([`(,.~@i.@#@])`])}  NB. stuff x into diagonal of y
lrtoa =: (((1. todiag *) +/ . * (* -.)) >/~@i.@#)

{{
NB. assert. (-: 1&{::@(128!:10)@:lrtoa) 3 3 $ 2 _1 _2 _2 4 _1 _2 _1 3
NB. assert. (-: 1&{::@(128!:10)@:lrtoa) 4 4  $ 2 _1 _2 5  _2 4 _1 3  _2 _1 3 2  2 1 _2 3
NB. ((>./@:,@:-) 1&{::@(128!:10)@:lrtoa)@:>:@:i.@:(,~)"0 i. 20
t128 =. {{ lrin (>./@:,@:|@:-) out128 =: 1 {:: 128!:10 lrtoa lrin =: y }}
assert. 1e_15 > >./ t128@(1.&todiag)@:(0.01&*)@:(0 ?@$~ ,~)"0 i. 25
assert. 1e_14 > >./ t128@:(1.&todiag)@:(0.01&*)@:(0 ?@$~ ,~)"0 ] 500 + 5 ?@$ 25
NB. assert. 1e_14 > >/. ((>./@:,@:|@:-) 1&{::@(128!:10)@:lrtoa)@:(0 ?@$~ ,~)"0 ] 500 + i. 25
NB. sparse matrices
assert. 1e_15 > >./ t128@:((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.01 * *:)"0 i. 25
assert. 1e_15 > >./ t128@:((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.001 * *:)"0 ] 500 + 5 ?@$  25
ckmat =. {{ if. 1e_10 < err =. x (>./@:,@:|@:- 1&{::@(128!:10)) y do. 13!:8 (4) ] errx =: x [ erry =: y end. err }}
NB. assert. 1e_14 > >./ (ckmat lrtoa)@:((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.01 * *:)"0 ] 500 + i. 50
NB. assert. 1e_14 > >./ (ckmat lrtoa)@:((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.1 * *:)"0 ] 500 + i. 50
assert. 1e_10 > >./ (ckmat lrtoa)@:((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.001 * *:)"0 ] 500 + 10 ?@$ 50

NB. permutation
for_dim. 2 + 20 ?@$ 40 do.
  'l r' =.  (10 * (2#dim) ?@$ 0) ((*-.) ,: *) t =. <:/~ i. dim
  l =. 1. todiag l * 0.5 > ($l) ?@$ 0
  a =. ({~ ?~@#) l +/ . * r
  'p lr' =. (1 0.01 1e_6 0) 128!:10 perma=: a
  assert. 1e_7 > >./ | , (p { a) - lrtoa lr
end.
1
}}^:IF64 1 NB. all J64 support EMU_AVX2 emulated fma

NB. LU rational
lrtoar =: (((1 todiag *) +/ . * (* -.)) >/~@i.@#)  NB. y is compressed Doolittle form, result is original a
(-: (0&{:: /:~ lrtoar@(1&{::))@(128!:10))@(1000x ?@$~ ,~)"0 i. 15

t=: 3 : 0''
if. (-.IF64) +. GITHUBCI*.('ARM64'-.@-:2!:5'RUNNER_ARCH')*.'arm64'-:(9!:56'cpu') do.
  EMPTY return.
end.
for_i. (>: , 500&+) (i.15) do.
 a1=. 128!:10 r=. (1000 ?@$~ ,~) i
 echo b=. >./ | ,r - (0&{:: /:~ lrtoa@(1&{::)) a1  NB. floating point
 assert. 1e_4 > b
end.
EMPTY
)

1: 0 : 0
sm =. ((1. todiag (2#[) $ (0.01 * ?@$&0@])`((? *:)~)`(0. #~ *:@[)})   [: <. 0.001 * *:) 1000
dm =.lrtoa@:(1.&todiag)@:(0.01&*)@:(0 ?@$~ ,~) 1000
)

NB. 128!:10 -------------------------------------------------------------
NB. lapack

t=: 3 : 0
N=. y
a=. (N,N) ?@$ 1000 1000
echo '$a ',":$a
t1=. 6!:2'c1=. 128!:10 a'
echo 'double  ',(' GFlop ',~ 0j3": (N^3)%(t1)*1e9),((N>:(9!:56'fma'){10,500)*.9!:56'cblas')#' cblas'
if. IF64 +. 9!:56'cblas' do.
  echo b=. >./ | ,a - (0&{:: /:~ lrtoa@(1&{::)) c1
  assert. 1e_4 >  b
end.

a=. a j. (N,N) ?@$ 1000 1000
t1=. 6!:2'c1=. 128!:10 a'
echo 'complex ',(' GFlop ',~ 0j3": 4*(N^3)%(t1)*1e9),((9!:56'cblas')#' cblas')
if. 9!:56'cblas' do.
  echo b=. >./ | ,a - (0&{:: /:~ lrtoa@(1&{::)) c1
  assert. 1e_4 >  b
end.
EMPTY
)

(3 : 0)^:(IFWIN*:-.IF64) ''
if. GITHUBCI*.('ARM64'-.@-:2!:5'RUNNER_ARCH')*.'arm64'-:(9!:56'cpu') do.
  EMPTY return.
end.
echo 9!:14''
echo '128!:10  cpu ',(9!:56'cpu'),' cores ',": {. 8 T. ''
t IF64{250 500
if. ((9!:56)'cblas') +. (9!:56)'fma' do. NB. otherwise too slow
  t IF64{500 1000
end.
EMPTY
)

NB. 128!:15 -------------------------------------------------------------
NB. xxhash
'ec5a19b0' -: 1 (128!:15) 'add12'
_1340515604 -: _1 (128!:15) 'add12'  NB. signed-extended

a=: 1e9$a.
'2fa73a07' -: 1(128!:15)a
'96b700eabf0aaaab' -: 2(128!:15)a
'84f143c4bac3c047' -: 3(128!:15)a
'84f143c4bac3c047ab4ecd6017e4b6f2' -: 4(128!:15)a
121284399 -: _1(128!:15)a
(IF64{::_369051754;_6077032927802116202) -: _2(128!:15)a   NB. j32 shorted to lower 32-bits
(IF64{::_1002180220;5170347579145449860) -: _3(128!:15)a
132 241 67 196 186 195 192 71 171 78 205 96 23 228 182 242 -: a.i. _4(128!:15)a

'12a6ab3a32f9f59c3b548e2159bf41b6' -: 4 (128!:15) 'add12'
'12a6ab3a32f9f59c3b548e2159bf41b6' -: (128!:15) 'add12'
'ba72533c008dec4ee2b76c43a79121b7' -: (128!:15) u:a.
'8e19e627079f8ae6124ab4163c9d9179' -: (128!:15) 1.2+ i.100 100
'54e6b8a109d513ea9d6fd6b7133d91be' -: (128!:15) (11 c. 1.2) + i.100 100
(IF64{::'6e7ba16fb23c685c508708ff7fbe816a';'e34c8e65da9f29af1a943c5bf392bc94') -: (128!:15) i.100 100

epilog''

